package com.suarte.webapp.action;

import com.suarte.core.Currency;
import com.suarte.core.ExchangeRateType;
import com.suarte.core.service.CurrencyManager;
import com.suarte.core.service.ExchangeRateManager;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.appfuse.service.GenericManager;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
 * @date   Mar 1, 2011
 * @author Ggutierrez
 */
public class ExchangeRateList extends BasePage implements Serializable {

    private ExchangeRateManager exchangeRateManager;
    private CurrencyManager currencyManager;
    private List exchangeRates;
    private Date date;
    private String stType;
    private Currency fromCurrency;
    private Currency toCurrency;

    public ExchangeRateManager getExchangeRateManager() {
        return exchangeRateManager;
    }

    public void setExchangeRateManager(ExchangeRateManager exchangeRateManager) {
        this.exchangeRateManager = exchangeRateManager;
    }

    public void setCurrencyManager(CurrencyManager currencyManager) {
        this.currencyManager = currencyManager;
    }    

    public ExchangeRateList() {
        setSortColumn("id"); // sets the default sort column
    }

    public String execute(){
        ExchangeRateType type = !isBlank(stType) ? ExchangeRateType.valueOf(stType) : null;
        exchangeRates = sort(exchangeRateManager.findRates(date, null, toCurrency, type));

        return "list";
    }

    public List getExchangeRates() {
        return exchangeRates;
    }

    public void setExchangeRates(List exchangeRates) {
        this.exchangeRates = exchangeRates;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getStType() {
        return stType;
    }

    public void setStType(String stType) {
        this.stType = stType;
    }

    public Currency getFromCurrency() {
        return fromCurrency;
    }

    public void setFromCurrency(Currency fromCurrency) {
        this.fromCurrency = fromCurrency;
    }

    public Currency getToCurrency() {
        return toCurrency;
    }

    public void setToCurrency(Currency toCurrency) {
        this.toCurrency = toCurrency;
    }

    public List<Currency> getCurrencies(){
        return currencyManager.findAllButLocal();
    }
}
